﻿import os 

from google.appengine.ext.webapp import template 
from google.appengine.api import users 
from google.appengine.ext import webapp 
from google.appengine.ext.webapp.util import run_wsgi_app 
from google.appengine.ext import db 
from datetime import datetime;
class Log(db.Model):
    content = db.StringProperty()
    date = db.DateTimeProperty(auto_now_add=True) 
 
class State():
    new = 0
    finished = 1    
 
class Note(db.Model): 
    author = db.UserProperty() 
    content = db.StringProperty()
    state = db.IntegerProperty()
    detail = db.StringProperty(multiline=True)
    date = db.DateTimeProperty(auto_now_add=True) 
 
class GetTasks(webapp.RequestHandler): 
    def get(self): 
        notes = Note.gql("ORDER BY date DESC").fetch(30)
        editable = self.request.get('editable')
        self.renderTasks(notes, editable);
        
    def renderTasks(self, notes, editable):    
        agent = self.request.headers.get('User-Agent')
        log = Log()
        log.content = agent
        log.put()
        if agent and agent.find("SymbianOS") > 0: 
            path = os.path.join(os.path.dirname(__file__), 'mobilePages/index.html') 
        elif agent and agent == "gzip(gfe)":
            path = os.path.join(os.path.dirname(__file__), 'mobilePages/index.html') 
        else:
            path = os.path.join(os.path.dirname(__file__), 'desktopPages/index.html')
             
        template_values = { 
            'notes': notes,
			'editable': editable,
            } 
        self.response.out.write(template.render(path, template_values))
 
class AddTask(webapp.RequestHandler): 
    def post(self): 
        note = Note() 
 
        if users.get_current_user(): 
            note.author = users.get_current_user() 
 
        note.content = self.request.get('content') 
        note.detail = self.request.get('detail') 
        note.state = State.new;
        note.put() 
        self.redirect('/?editable=true') 
        
class FinishTask(webapp.RequestHandler): 
    def get(self):
        id = self.request.get('key') 
        note = Note.get_by_id(int(id))
        note.state = State.finished
        note.date = datetime.now();
        note.put()
        self.redirect('/?editable=true') 

class AutoTask(webapp.RequestHandler): 
    def get(self):
        note = Note()
        if users.get_current_user(): 
            note.author = users.get_current_user() 
        note.content = 'do gym'
        note.state = State.new
        note.put()
        self.redirect('/?editable=true')

class Recover(webapp.RequestHandler):
    def get(self):
        self.patch()
        
    def patch(self):
        notes = Note.all()
        for note in notes:
            note.state = State.new
            note.put()
        self.redirect('/?editable=true')
        
application = webapp.WSGIApplication(
                                     [('/', GetTasks),
                                      ('/tasks', AddTask),
                                      ('/recover', Recover),
                                      ('/tasks/finish', FinishTask),
                                      ('/tasks/auto/gym', AutoTask)],
                                     debug=True) 
 
def main(): 
    webapp.template.register_template_library('filter')
    run_wsgi_app(application) 
 
if __name__ == "__main__": 
    main()

